<template>
	<view class="product">
		<watermark></watermark>
		<view class="pad20">
			<form @submit="formSubmit">
				<view class="zyjz space-between f28 pad-t-30 pad-b-30 border-b">
					<text class="clAAA">产品名称</text>
					<input class="uni-input" placeholder="请输入" v-model="dataValue.goods_name"
						:disabled="infoData.type !== 'admin'" />
				</view>
				<view v-if="id" class="zyjz space-between f28 pad-t-30 pad-b-30 border-b">
					<text class="clAAA">库存</text>
					<input class="uni-input" placeholder="请输入" v-model="dataValue.num" />
				</view>
				<view class="zyjz space-between f28 pad-t-30 pad-b-30 border-b">
					<text class="clAAA">图片</text>
					<view class="">
						<uni-file-picker v-model="img" return-type="object" :limit="1" fileMediatype="image"
							:imageStyles="imageStyles" mode="grid" @select="selectImg" />
						<!-- :readonly="infoData.type !== 'admin'" -->
					</view>
				</view>
				<view class="zyjz space-between f28 pad-t-30 pad-b-30 border-b">
					<text class="clAAA">分类</text>
					<uni-data-picker v-model="dataValue.cat_id" :map="{text:'name',value:'id'}" :localdata="treeCatList"
						popup-title="请选择" @change="onchange" :readonly="infoData.type !== 'admin'"></uni-data-picker>
				</view>
				<view class="zyjz space-between f28 pad-t-30 pad-b-30 border-b" v-if="infoData.type === 'admin'">
					<text class="clAAA">金额</text>
					<input class="uni-input" placeholder="请输入" v-model="dataValue.price" />
				</view>
				<!-- <view class="zyjz space-between f28 pad-t-30 pad-b-30 border-b">
					<text class="clAAA">仓库</text>
					<uni-data-select v-model="dataValue.price" :localdata="warehouseList" @change="change"></uni-data-select>
				</view> -->
			</form>

		</view>
		<view class="bottom zyjz pad-l-40 pad-t-20 pad-b-20 pad-r-20 bgfff f28 cl343">
			<button style="width: 100%;" type="primary" hover-class="none" @click="sub">确定</button>
		</view>
	</view>
</template>

<script>
	import watermark from '@/components/watermark.vue'
	import mixins from '@/mixins/index.js'
	import business from '@/mixins/business.js'
	export default {
		mixins: [mixins, business],
		components: {
			watermark
		},
		data() {
			return {
				imageStyles: {
					width: 64,
					height: 64,
				},
				id: '',
				w_id: '',
				dataValue: {
					goods_name: '',
					price: '',
					cat_id: '',
					num: ''
				},
				img: {},
			}
		},
		onLoad(e) {
			if (e && e.id) {
				this.id = e.id
				this.w_id = e.w_id
				this.getProduct()
			}
			this.getCat(() => {
				for (let i in this.treeCatList) {
					this.$set(this.treeCatList[i], 'children', this.treeCatList[i].item)
				}
			})
		},
		methods: {
			selectImg(e) {
				this.img = e.tempFiles[0].file
				this.img.url = e.tempFiles[0].url
			},
			sub() {
				if (this.infoData.type === 'admin') {
					if (this.id) {
						const obj = {
							input: {
								goods_id: this.id,
								goods_name: this.dataValue.goods_name,
								price: this.dataValue.price,
								cat_id: this.dataValue.cat_id,
								num: this.dataValue.num,
								w_id: this.w_id,
								img: ''
							}
						}
						console.log("this.img", this.img)


						if (this.img.url.indexOf(this.api.url) != -1) {

							obj.input.img = this.img
							this.api.post('edit_goods', obj).then(res => {
								this.api.msg(res.msg)
								setTimeout(() => {
									this.back()
								}, 2000)
							})

						} else {
							this.api.upload([this.img]).then(res => {
								obj.input.img = res.data[0].path
								this.api.post('edit_goods', obj).then(res => {
									this.api.msg(res.msg)
									setTimeout(() => {
										this.back()
									}, 2000)
								})
							})
						}

					} else {
						const obj = {
							input: {
								goods_name: this.dataValue.goods_name,
								price: this.dataValue.price,
								cat_id: this.dataValue.cat_id,
								num: this.dataValue.num,
								img: ''
							}
						}
						this.api.upload([this.img]).then(res => {
							obj.input.img = res.data[0].path
							this.api.post('add_goods', obj).then(res => {
								this.api.msg(res.msg)
								setTimeout(() => {
									this.back()
								}, 2000)
							})
						})
					}
				} else {
					const obj = {
						goods_id: this.id,
						w_id: this.w_id,
						num: this.dataValue.num
					}
					this.api.post('edit_num', obj).then(res => {
						this.api.msg(res.msg)
						setTimeout(() => {
							this.back()
						}, 2000)
					})
				}
			},
			getProduct() {
				const obj = {
					goods_id: this.id,
					w_id: this.w_id
				}
				this.api.get('goods_details', obj).then(res => {
					this.dataValue = res.data
					this.img.url = res.data.img
					this.dataValue.num = !this.dataValue.num ? 0 : this.dataValue.num
				})
			}
		}
	}
</script>

<style lang="less">
	.product {
		input {
			text-align: right;

		}

		.border-b {
			border-bottom: 1px solid #f7f7f7;
		}
	}
</style>